<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-24
 * Time: 10:49
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
use qcos\Cos;

class VersionController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $a_type = isset($param['a_type']) ? $param['a_type'] : 0;
        $a_status = isset($param['a_status']) ? $param['a_status'] : 0;
        $where = null;
        $advertising = DB::name('version')
            ->where($where)
            ->order(['id' => 'DESC'])
            ->paginate(20);
        $advertising->appends($param);
        $page = $advertising->render();
        $this->assign("a_type", $a_type);
        $this->assign("a_status", $a_status);
        $this->assign("advertising", $advertising);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        $this->assign("version", VERSION_LIVESTAR);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'v_number' => 'require|alphaNum',
                'v_digital' => 'require',
                'v_forcibly' => 'require|number',
                'v_updatetime' => 'require|dateFormat:Y-m-d H:i|after:+1 day|before:+1 month',
                'v_description' => 'require|min:8|max:128',
                'v_type' => 'require|number',
                'v_link' => 'require|min:16|max:128',
            ]);
            $validate->message([
                'v_number.require' => '接口版本不能为空',
                'v_number.alphaNum' => '接口版本能为字母和数字',
                'v_digital.require' => '版本号不可为空',
                'v_forcibly.require' => '请选择更新类型',
                'v_forcibly.number' => '更新类型不正确',
                'v_updatetime.require' => '请填写上线日期',
                'v_updatetime.dateFormat' => '上线日期格式不正确',
                'v_updatetime.after' => '上线日期时间过短',
                'v_updatetime.before' => '上线日期时间过长',
                'v_description.number' => '发版说明不可为空',
                'v_description.min' => '发版说明最小长度为8个字符',
                'v_description.max' => '发版说明最大长度为128个字符',
                'v_type.require' => '请选择版本类型',
                'v_type.number' => '版本类型不正确',
                'v_link.require' => '下载链接不可为空',
                'v_link.min' => '下载链接最小长度为16个字符',
                'v_link.max' => '下载链接最大长度为128个字符',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                if($data['v_type'] == 20){
                    $path = $data['path'];
                    $file = $data['file'];
                    if($path && $file){
                        $data['v_link'] = $this->moveImg($path,$file,7);
                    }else{
                        $this->error('请上传apk文件');
                    }
                }
                unset($data['file']);
                unset($data['path']);
                $data['v_userid'] = cmf_get_current_admin_id();
                Db::name('version')->insert($data);
                $this->success('添加成功',url('version/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $version = DB::name('version')->where(["id" => $id])->find();
        $this->assign("version", $version);
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'id' => 'require|number',
                'v_number' => 'require|alphaNum',
                'v_digital' => 'require',
                'v_forcibly' => 'require|number',
                'v_updatetime' => 'require|dateFormat:Y-m-d H:i|after:+1 day|before:+1 month',
                'v_description' => 'require|min:8|max:128',
                'v_type' => 'require|number',
                'v_link' => 'require|min:16|max:128',
            ]);
            $validate->message([
                'v_number.require' => '接口版本不能为空',
                'v_number.alphaNum' => '接口版本能为字母和数字',
                'v_digital.require' => '版本号不可为空',
                'v_forcibly.require' => '请选择更新类型',
                'v_forcibly.number' => '更新类型不正确',
                'v_updatetime.require' => '请填写上线日期',
                'v_updatetime.dateFormat' => '上线日期格式不正确',
                'v_updatetime.after' => '上线日期时间过短',
                'v_updatetime.before' => '上线日期时间过长',
                'v_description.number' => '发版说明不可为空',
                'v_description.min' => '发版说明最小长度为8个字符',
                'v_description.max' => '发版说明最大长度为128个字符',
                'v_type.require' => '请选择版本类型',
                'v_type.number' => '版本类型不正确',
                'v_link.require' => '下载链接不可为空',
                'v_link.min' => '下载链接最小长度为16个字符',
                'v_link.max' => '下载链接最大长度为128个字符',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                if($data['v_type'] == 20){
                    $path = $data['path'];
                    $file = $data['file'];
                    if($path && $file){
                        $fileUrl = parse_url($data['link']);
                        $hostUrl = parse_url(HTTP_IMG);
                        if(!empty($fileUrl['host']) && !empty($hostUrl['host']) && ($fileUrl['host'] == $hostUrl['host'])){
                            $this->deleteImg($fileUrl['path']);
                        }
                        $data['v_link'] = $this->moveImg($path,$file,7);
                    }
                }
                unset($data['link']);
                unset($data['file']);
                unset($data['path']);
                $data['v_userid'] = cmf_get_current_admin_id();
                Db::name('version')->update($data);
                $this->success('更新成功',url('version/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function uploadFile()
    {
        $file = $_FILES['file'];
        $root = VERSION_LIVESTAR.'/Default/';
        $pathName['path'] = date('Y/m/');
        $pathName['name'] = microtime (true)*10000;
        $path = $root.$pathName['path'];
        $cos = new Cos();
        $type = pathinfo($file["name"]);
        $info = $cos->upload($file["tmp_name"],$path.$pathName['name'].'.'.$type['extension']);
        if($info){
            $data['code'] = 1;
            $data['msg'] = '上传成功';
            $data['data'] = ['path' => $pathName['path'],'file' => $pathName['name'].'.'.$type['extension']];
        }else{
            $data['code'] = 0;
            $data['msg'] = '上传错误';
            $data['data'] = [];
        }
        return json_encode($data);

    }
}